#include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
struct DSU {
    vector<int> parent, size;
    DSU(int n) {
        parent.resize(n + 1);
        size.resize(n + 1, 1);
        for(int i = 1; i <= n; i++) {
            parent[i] = i;
        }
    }
    int find(int x) {
        if(parent[x] != x) {
            return parent[x] = find(parent[x]);
        }
        return parent[x];
    }
    void unite(int x, int y) {
        int fx = find(x), fy = find(y);
        if(fx == fy) {
            return;
        }
        if(size[fx] < size[fy]) {
            swap(fx, fy);
        }
        parent[fy] = fx;
        size[fx] += size[fy];
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
};
int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        DSU a(200010);
        DSU b(200010);
        int k = 1;
        int flag = 0;
        unordered_map<int, int> h;
        unordered_map<int, bool> hp;
        vector<pair<int, int>> nums;
        for(int i = 0; i < n; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            if(!h[x])
            {
               h[x] = k++; 
            }
            if(!h[y]) {
                h[y] = k++;
            }
            if(z == 1) 
            {
                a.unite(h[x], h[y]);
            }
            else 
            {
                nums.push_back(make_pair(x, y));
                if(h[x] == h[y]) {
                    hp[h[x]] = true;
                }
                b.unite(h[x], h[y]);
            }
        }
        for(auto&[l, r] : nums) {
                if(a.same(h[l], h[r]) && h[l] != h[r]) {
                    flag = 1;
                }else if(h[l] == h[r] && hp[h[l]]) {
                    flag = 1;
                }
            }
        if(!flag) {
            cout << "YES" << endl;
        }else {
            cout << "NO" << endl;
        }
    }
    return 0;
}